﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CR.ErrorCode
{
    /// <summary>
    /// API结果封装类
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public ApiEnums Code { get; set; } = ApiEnums.Fail;

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = "失败";

        /// <summary>
        /// 数据
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; } = DateTimeOffset.Now.ToUnixTimeMilliseconds();

        /// <summary>
        /// 设置成功状态
        /// </summary>
        /// <param name="message">成功消息</param>
        public void SetSuccess(string message = "成功")
        {
            this.Message = message;
            this.Code = ApiEnums.Success;
        }

        /// <summary>
        /// 设置失败状态
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        public void SetFail(string message = "失败", ApiEnums code = ApiEnums.Fail)
        {
            this.Message = message;
            this.Code = code;
        }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="message">成功消息</param>
        /// <param name="data">数据</param>
        /// <returns>API结果</returns>
        public static ApiResult Success(string message = "成功", object data = null)
        {
            return new ApiResult
            {
                Code = ApiEnums.Success,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建成功结果（兼容方法，同Success）
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">成功消息</param>
        /// <returns>API结果</returns>
        public static ApiResult Ok(object data = null, string message = "成功")
        {
            return Success(message, data);
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <returns>API结果</returns>
        public static ApiResult Fail(ApiEnums code, string message)
        {
            return new ApiResult
            {
                Code = code,
                Message = message
            };
        }

        /// <summary>
        /// 创建失败结果（兼容方法）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns>API结果</returns>
        public static ApiResult Error(string message, ApiEnums code = ApiEnums.Fail)
        {
            return Fail(code, message);
        }

        /// <summary>
        /// 检查是否成功
        /// </summary>
        /// <returns>是否成功</returns>
        public bool IsSuccess()
        {
            return Code == ApiEnums.Success;
        }
    }

    /// <summary>
    /// 泛型API结果封装类
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ApiResult<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public ApiEnums Code { get; set; } = ApiEnums.Fail;

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = "失败";

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; } = DateTimeOffset.Now.ToUnixTimeMilliseconds();

        /// <summary>
        /// 设置成功状态
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">成功消息</param>
        public void SetSuccess(T data, string message = "成功")
        {
            this.Message = message;
            this.Data = data;
            this.Code = ApiEnums.Success;
        }

        /// <summary>
        /// 设置失败状态
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        public void SetFail(string message = "失败", ApiEnums code = ApiEnums.Fail)
        {
            this.Message = message;
            this.Code = code;
        }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns>API结果</returns>
        public static ApiResult<T> Success(T data, string message = "成功")
        {
            return new ApiResult<T>
            {
                Code = ApiEnums.Success,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建成功结果（兼容方法，同Success）
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">成功消息</param>
        /// <returns>API结果</returns>
        public static ApiResult<T> Ok(T data, string message = "成功")
        {
            return Success(data, message);
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <returns>API结果</returns>
        public static ApiResult<T> Fail(ApiEnums code, string message)
        {
            return new ApiResult<T>
            {
                Code = code,
                Message = message
            };
        }

        /// <summary>
        /// 创建失败结果（兼容方法）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns>API结果</returns>
        public static ApiResult<T> Error(string message, ApiEnums code = ApiEnums.Fail)
        {
            return Fail(code, message);
        }

        /// <summary>
        /// 从非泛型结果转换
        /// </summary>
        /// <param name="result">非泛型API结果</param>
        /// <returns>泛型API结果</returns>
        public static ApiResult<T> From(ApiResult result)
        {
            return new ApiResult<T>
            {
                Code = result.Code,
                Message = result.Message,
                Data = result.Data != null ? (T)result.Data : default
            };
        }

        /// <summary>
        /// 检查是否成功
        /// </summary>
        /// <returns>是否成功</returns>
        public bool IsSuccess()
        {
            return Code == ApiEnums.Success;
        }
    }
}
